package com.example.aidlclientdemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SocketActivity extends AppCompatActivity {

    private SocketServer server;

    private SocketClient client;

    private Button btnStartServer, btnConnect, btnSend;

    private EditText etMessage, etServerIp;

    private TextView tvLog;

    public static void start(Context context) {
        Intent starter = new Intent(context, SocketActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket);
        // 初始化UI组件
        btnStartServer = findViewById(R.id.btnStartServer);
        btnConnect = findViewById(R.id.btnConnect);
        btnSend = findViewById(R.id.btnSend);
        etMessage = findViewById(R.id.etMessage);
        etServerIp = findViewById(R.id.etServerIp);
        tvLog = findViewById(R.id.tvLog);
        // 启动服务器
        btnStartServer.setOnClickListener(v -> {
            server = new SocketServer();
            server.start();
            logMessage("Server started on port 8888");
        });
        // 连接到服务器
        btnConnect.setOnClickListener(v -> {
            String ip = etServerIp.getText().toString().trim();
            if (ip.isEmpty()) {
                ip = "127.0.0.1"; // 模拟器本地回环地址
            }
            client = new SocketClient();
            client.connect(ip, 8888, new SocketClient.MessageCallback() {
                @Override
                public void onConnected() {
                    runOnUiThread(() -> logMessage("Connected to server"));
                }

                @Override
                public void onConnectionFailed() {
                    runOnUiThread(() -> logMessage("Connection failed"));
                }

                @Override
                public void onDisconnected() {
                    runOnUiThread(() -> logMessage("Disconnected"));
                }

                @Override
                public void onMessageReceived(String message) {
                    runOnUiThread(() -> logMessage("Received: " + message));
                }
            });
        });
        // 发送消息
        btnSend.setOnClickListener(v -> {
            String msg = etMessage.getText().toString();
            if (client != null && !msg.isEmpty()) {
                new Thread(() -> {
                    client.sendMessage(msg);
                    runOnUiThread(() -> {
                        logMessage("Sent: " + msg);
                        etMessage.setText("");
                    });
                }).start();
            }
        });
    }

    private void logMessage(String message) {
        tvLog.append(message + "\n");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (server != null) {
            server.stop();
        }
        if (client != null) {
            client.disconnect();
        }
    }

}